package org.monkey.common.constant.util;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import org.monkey.common.constant.entity.ErrorResultEnum;
import org.monkey.common.constant.exception.BaseException;

/**
 * 统一的io处理工具类
 *
 * @author liting
 */
public class IoUtil {

  /** 默认的字节读取大小 */
  private static final int SIZE = 4 * 1024;

  public static File inputStream2File(
      InputStream inputStream, String targetFile, boolean closeStream) throws IOException {
    File file = new File(targetFile);
    String fn = file.getName();
    File parentFile = file.getParentFile();
    String newFilePath = file.getParent() + "/" + fn;
    File newFile = new File(newFilePath);
    OutputStream outStream = null;
    try {
      outStream = new FileOutputStream(newFile);
      if (!parentFile.exists()) {
        parentFile.mkdirs();
      }
      byte[] buffer = new byte[SIZE];
      int bytesRead;
      while ((bytesRead = inputStream.read(buffer)) != -1) {
        outStream.write(buffer, 0, bytesRead);
      }
      outStream.flush();
    } finally {
      if (outStream != null) {
        outStream.close();
      }
      if (closeStream && inputStream != null) {
        inputStream.close();
      }
    }
    return newFile;
  }

  private static String readFile(String fileName) {
    int len = 0;
    StringBuffer str = new StringBuffer("");
    File file = new File(fileName);
    try {
      FileInputStream is = new FileInputStream(file);
      InputStreamReader isr = new InputStreamReader(is);
      BufferedReader in = new BufferedReader(isr);
      String line = null;
      while ((line = in.readLine()) != null) {
        if (len != 0) {
          str.append("\r\n").append(line);
        } else {
          str.append(line);
        }
        len++;
      }
      in.close();
      is.close();
    } catch (IOException e) {
      e.printStackTrace();
      throw new BaseException(ErrorResultEnum.SYSTEM_ERROR);
    }
    return str.toString();
  }

  /** 目的是检查目录存不存在，不存在则创建. */
  public static void checkPath(String path) {
    File file = new File(path);
    if (file.isDirectory() && !file.exists()) {
      file.mkdirs();
    }
    if (!file.exists()) {
      file.mkdirs();
    }
  }

  /** byte数组转文件 */
  public static byte[] file2Byte(File file) throws IOException {
    byte[] buffer;
    FileInputStream fis = null;
    ByteArrayOutputStream bos = null;
    try {
      checkPath(file.getParent());
      fis = new FileInputStream(file);
      bos = new ByteArrayOutputStream();
      byte[] b = new byte[SIZE];
      int n;
      while ((n = fis.read(b)) != -1) {
        bos.write(b, 0, n);
      }
    } finally {
      if (fis != null) {
        fis.close();
      }
      if (bos != null) {
        bos.close();
      }
    }
    buffer = bos.toByteArray();
    return buffer;
  }
}
